package com.jinkeen.dddk

import ACTION_ALARM_REPLENISH_STOCK
import ACTION_ALARM_SYNCHRONIZE
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.Toast

/**
 * 闹钟 定时任务
 */
class AlarmReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        when (intent.action) {
            ACTION_ALARM_SYNCHRONIZE -> doSynchronizeAction(context)
            ACTION_ALARM_REPLENISH_STOCK -> doReplenishStockAction(context)
        }
    }

    private fun doSynchronizeAction(context: Context?) {
        // TODO: 2021/12/29  
    }

    /**
     * 执行补充动作 需要做的事情
     */
    private fun doReplenishStockAction(context: Context?) {
        // TODO: 2021/12/29
        // com.alibaba.android.rimet.biz.LaunchHomeActivity
        openDD(context)
    }

    val packageName = "com.alibaba.android.rimet.biz.LaunchHomeActivity"
    private fun openDD(context: Context?) {
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        intent.setPackage(packageName)
        val apps = context?.packageManager?.queryIntentActivities(intent, 0)
        if (apps!!.size > 0) {
            val packageName = apps[0].activityInfo.packageName
            val className = apps[0].activityInfo.name
            val cn = ComponentName(packageName, className)
            intent.setComponent(cn)
            context.startActivity(intent)
        } else {
            Toast.makeText(context, "${packageName}未安装",Toast.LENGTH_LONG).show()
        }
    }
}